import {
  createRouter,
  createWebHistory,
  createWebHashHistory,
  RouteRecordRaw,
} from "vue-router";
// import loading from "@/assets/img/logo.gif";

import { useLoading } from "@/hooks/useloadingpage";
const { isLoading, startLoading, finishLoading } = useLoading();
import { getToken } from "@/hooks/usegettoken";
// import { useStore } from "vuex";
// const store = useStore();
import store from "@/store";
// import media from "@/assets/img/media.png";
// import data from "@/assets/img/data.png";
// import order from "@/assets/img/order.png";
// import money from "@/assets/img/money.png";
// import img1 from "@/assets/img/account.png";

const routes: RouteRecordRaw[] = [
  {
    path: "/",
    component: () => import("@/layout/index.vue"),
    redirect: "/home",
    name: "home",
    meta: {
      title: "首页",
      path: "/",
      keepAlive: true, //设置页面是否需要使用缓存
    },
    children: [
      {
        path: "/home",
        component: () => import("@/views/home/home.vue"),
        name: "home",
        meta: {
          title: "首页",
          path: "/home",
          keepAlive: true, //设置页面是否需要使用缓存
        },
      },
      {
        path: "/Star",
        name: "Star",
        component: () => import("@/views/Star/Star.vue"),
        meta: {
          title: "活动",
          path: "/Star",
          keepAlive: true, //设置页面是否需要使用缓存
        },
      },
      // {
      //   path: "/Treasure",
      //   name: "Treasure",
      //   component: () => import("@/views/Treasure/index.vue"),
      //   meta: {
      //     title: "分享赚钱",
      //     path: "/Treasure",
      //   },
      // },
      {
        path: "/Share",
        name: "Share",
        component: () => import("@/views/Share/Share.vue"),
        meta: {
          title: "宝箱",
          path: "/Share",
          keepAlive: true, //设置页面是否需要使用缓存
        },
      },
      {
        path: "/My",
        name: "My",
        component: () => import("@/views/My/My.vue"),
        meta: {
          title: "我的",
          path: "/My",
          keepAlive: true, //设置页面是否需要使用缓存
        },
      },
      // {
      //   path: "/hotgame",
      //   component: () => import("@/views/home/children/hothome.vue"),
      //   meta: {
      //     title: "热门游戏",
      //   },
      // },
      // {
      //   path: "/history",
      //   component: () => import("@/views/home/children/history.vue"),
      //   meta: {
      //     title: "历史",
      //   },
      // },
      // {
      //   path: "/slot",
      //   component: () => import("@/views/home/children/slot.vue"),
      //   meta: {
      //     title: "分类",
      //   },
      // },
      // {
      //   path: "/livehouse",
      //   component: () => import("@/views/home/children/livehouse.vue"),
      //   meta: {
      //     title: "直播",
      //   },
      // },
      // {
      //   path: "/board",
      //   component: () => import("@/views/home/children/board.vue"),
      //   meta: {
      //     title: "",
      //   },
      // },
      // {
      //   path: "/fishing",
      //   component: () => import("@/views/home/children/fishing.vue"),
      //   meta: {
      //     title: "",
      //   },
      // },
      {
        path: "/commonpage",
        name: "commonpage",
        component: () => import("@/views/home/children/slothash.vue"),
        meta: {
          title: "",
          path: "/commonpage",
          keepAlive: true, //设置页面是否需要使用缓存
        },
      },
      // {
      //   path: "/slottime",
      //   component: () => import("@/views/home/children/slottime.vue"),
      //   meta: {
      //     title: "",
      //   },
      // },
      // {
      //   path: "/slotchain",
      //   component: () => import("@/views/home/children/slotchain.vue"),
      //   meta: {
      //     title: "",
      //   },
      // },
    ],
  },
  {
    path: "/informcao",
    name: "informcao",
    component: () => import("@/views/home/children/informcao.vue"),
    meta: {
      title: "详情",
      path: "/informcao",
    },
  },
  {
    path: "/order",
    name: "order",
    component: () => import("@/views/home/children/order.vue"),
    meta: {
      title: "详情",
      path: "/order",
    },
  },
  {
    path: "/StarDetail",
    name: "StarDetail",
    component: () => import("@/views/Star/children/detail.vue"),
    meta: {
      title: "详情",
      path: "/StarDetail",
      keepAlive: true, //设置页面是否需要使用缓存
    },
  },
  {
    path: "/task",
    name: "task",
    component: () => import("@/views/home/children/task.vue"),
    meta: {
      title: "详情",
      path: "/task",
      keepAlive: true, //设置页面是否需要使用缓存
    },
  },
  {
    path: "/AmountDetail",
    name: "AmountDetail",
    component: () => import("@/views/My/children/AmountDetail.vue"),
    meta: {
      title: "详情",
      path: "/AmountDetail",
      keepAlive: false, //设置页面是否需要使用缓存
    },
  },
  {
    path: "/business",
    name: "business",
    component: () => import("@/views/home/children/business.vue"),
    meta: {
      title: "",
      path: "/business",
      keepAlive: true, //设置页面是否需要使用缓存
    },
  },
  {
    path: "/kefu",
    name: "kefu",
    component: () => import("@/views/kefu/index.vue"),
    meta: {
      title: "客服",
      path: "/kefu",
      keepAlive: false, //设置页面是否需要使用缓存
    },
  },
  {
    path: "/betting",
    name: "betting",
    component: () => import("@/views/My/children/betting.vue"),
    meta: {
      title: "",
      path: "/betting",
      keepAlive: true, //设置页面是否需要使用缓存
    },
  },
  {
    path: "/info",
    name: "info",
    component: () => import("@/views/My/children/info.vue"),
    meta: {
      title: "",
      path: "/info",
      keepAlive: true, //设置页面是否需要使用缓存
    },
  },
  {
    path: "/facebook",
    name: "facebook",
    component: () => import("@/views/My/children/facebook.vue"),
    meta: {
      title: "",
      path: "/facebook",
      keepAlive: true, //设置页面是否需要使用缓存
    },
  },
  {
    path: "/message",
    name: "message",
    component: () => import("@/views/My/children/message.vue"),
    meta: {
      title: "",
      keepAlive: true, //设置页面是否需要使用缓存
    },
  },
  {
    path: "/tran",
    name: "tran",
    component: () => import("@/views/My/children/tran.vue"),
    meta: {
      title: "",
      keepAlive: true, //设置页面是否需要使用缓存
    },
  },

  {
    path: "/language",
    name: "language",
    component: () => import("@/views/home/children/language.vue"),
    meta: {
      title: "登入",
      keepAlive: true, //设置页面是否需要使用缓存
    },
  },
  {
    path: "/wallet",
    name: "wallet",
    component: () => import("@/views/My/children/wallet.vue"),
    meta: {
      title: "钱包",
      keepAlive: true, //设置页面是否需要使用缓存
    },
  },
  {
    path: "/rechange",
    name: "rechange",
    component: () => import("@/views/My/children/rechange.vue"),
    meta: {
      title: "充值",
      keepAlive: true, //设置页面是否需要使用缓存
    },
  },
  {
    path: "/withdrawal",
    name: "withdrawal",
    component: () => import("@/views/My/children/withdrawal.vue"),
    meta: {
      title: "取款",
      keepAlive: true, //设置页面是否需要使用缓存
    },
  },
  {
    path: "/bonus",
    name: "bonus",
    component: () => import("@/views/My/children/bonus.vue"),
    meta: {
      title: "彩金钱包",
      keepAlive: true, //设置页面是否需要使用缓存
    },
  },
  {
    path: "/Account",
    name: "Account",
    component: () => import("@/views/My/children/Account.vue"),
    meta: {
      title: "账户",
      keepAlive: true, //设置页面是否需要使用缓存
    },
  },
  {
    path: "/vip",
    name: "vip",
    component: () => import("@/views/My/children/vip.vue"),
    meta: {
      title: "vip",
      keepAlive: true, //设置页面是否需要使用缓存
    },
  },
  {
    path: "/playGame",
    name: "playGame",
    component: () => import("@/views/home/children/playGame.vue"),
    meta: {
      title: "/playGame",
      keepAlive: false, //设置页面是否需要使用缓存
    },
  },
  {
    path: "/download",
    name: "download",
    component: () => import("@/views/home/children/download.vue"),
    meta: {
      title: "vip",
      keepAlive: true, //设置页面是否需要使用缓存
    },
  },
  {
    path: "/:error(.*)",
    name: "error",
    component: () => import("@/views/404/404.vue"),
    meta: {
      title: "404",
    },
  },
];
const router = createRouter({
  history: createWebHashHistory(),
  routes,
  scrollBehavior() {
    // 始终滚动到顶部
    return {
      top: 0,
      left: 0,
    };
  },
});
// let tag = document.createElement("div");
// let img = document.createElement("img");
// tag.className = "loading-page";
// tag.appendChild(img);
// document.body.appendChild(tag)
router.beforeEach((to, from, next) => {
  // console.log(to, 9999);
  // 路由白名单
  let whiteList = ["/", "/home", "/Star", "/My", "/language", "/kefu"];
  //配置token
  store.commit("SET_TOKEN", localStorage.getItem("token"));
  //设置用户信息
  store.commit("SET_INFO", JSON.parse(localStorage.getItem("info")));
  // 配置金币类型
  store.commit("SET_COINTYPE", localStorage.getItem("coinType"));
  //配置金币信息
  store.commit("SET_COININFO", JSON.parse(localStorage.getItem("coininfo")));
  

  // if (["/", "/home"].includes(to.fullPath)) {
  //   startLoading();
  // }
  // next();
  if (getToken() || whiteList.includes(to.fullPath)) {
    if (["/", "/home", "/playGame"].includes(to.fullPath)) {
      startLoading();
    }
    // startLoading();
    next();
  } else {
    store.commit("SET_LOGIN", true);
  }
});

router.afterEach((to, from) => {
  // let removetag = document.getElementsByClassName("loading-page")[0];
  // document.body.removeChild(removetag);
  finishLoading();
});
export default router;
